package cn.sdjzu.exercise;

import cn.sdjzu.exercise.db.DBHelper;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class PersonalInfoActivity extends BasedActivity implements
		OnClickListener {

	private TextView mTitle;
	private EditText mUsrNm;
	private RadioGroup mUsrSex;
	private EditText mUsrAge;
	private EditText mUsrHeight;
	private EditText mUsrWeight;
	private EditText mUsrFatcontent;
	private Button mRegBtn;
	private Button mClearBtn;
	private DBHelper dbHelper;
	ExerciseApp app;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExerciseApp.getInstance().addActivity(this);
		setContentView(R.layout.personal_info);
		app = (ExerciseApp) PersonalInfoActivity.this.getApplication();
		dbHelper = new DBHelper(this);
		mTitle = (TextView) this.findViewById(R.id.app_reg_title);
		mTitle.setText(R.string.app_update_info_title);
		mUsrNm = (EditText) this.findViewById(R.id.reg_edit_usr_name);
		mUsrSex = (RadioGroup) this.findViewById(R.id.reg_edit_sexRg);
		mUsrAge = (EditText) this.findViewById(R.id.reg_edit_usr_age);
		mUsrHeight = (EditText) this.findViewById(R.id.reg_edit_usr_height);
		mUsrWeight = (EditText) this.findViewById(R.id.reg_edit_usr_weight);
		mUsrFatcontent = (EditText) this
				.findViewById(R.id.reg_edit_usr_fatcontent);
		mRegBtn = (Button) this.findViewById(R.id.edit_save);
		mClearBtn = (Button) this.findViewById(R.id.edit_back);
		mRegBtn.setOnClickListener(this);
		mClearBtn.setOnClickListener(this);

		// 根据DB取得的User信息，更新画面上各个输入框的默认值
		mUsrNm.setText(dbHelper.getDataFromDB(DBHelper.TBL_USER,
				DBHelper.TBL_USER_NAME, app.currentCardNo));
		String sex = dbHelper.getDataFromDB(DBHelper.TBL_USER,
				DBHelper.TBL_USER_SEX, app.currentCardNo);
		if ("1".equals(sex)) {
			mUsrSex.check(R.id.reg_usr_sex_female);
		} else {
			mUsrSex.check(R.id.reg_usr_sex_male);
		}
		mUsrAge.setText(dbHelper.getDataFromDB(DBHelper.TBL_USER,
				DBHelper.TBL_USER_AGE, app.currentCardNo));
		mUsrHeight.setText(dbHelper.getDataFromDB(DBHelper.TBL_USER,
				DBHelper.TBL_USER_HEIGHT, app.currentCardNo));
		mUsrWeight.setText(dbHelper.getDataFromDB(DBHelper.TBL_USER,
				DBHelper.TBL_USER_WEIGHT, app.currentCardNo));
		mUsrFatcontent.setText(dbHelper.getDataFromDB(DBHelper.TBL_USER,
				DBHelper.TBL_USER_FATCONTENT, app.currentCardNo));
		// dbHelper.close();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.edit_save:
			boolean flg = checkInputData();
			if (flg) {
				ContentValues values = new ContentValues();
				values.put(DBHelper.TBL_USER_CARDNO, app.currentCardNo);
				// User name
				String name = mUsrNm.getText().toString().trim();
				values.put(DBHelper.TBL_USER_NAME, name);

				// User sex
				String sex = "";
				boolean isMale = ((RadioButton) findViewById(R.id.reg_usr_sex_male))
						.isChecked();
				if (isMale) {
					sex = "0";
				} else {
					sex = "1";
				}
				values.put(DBHelper.TBL_USER_SEX, sex);
				values.put(DBHelper.TBL_USER_AGE, mUsrAge.getText().toString()
						.trim());

				// User height
				String height = mUsrHeight.getText().toString().trim();
				values.put(DBHelper.TBL_USER_HEIGHT, height);

				// User weight
				String weight = mUsrWeight.getText().toString().trim();
				values.put(DBHelper.TBL_USER_WEIGHT, weight);

				// User fat content
				String fatcontent = mUsrFatcontent.getText().toString().trim();
				values.put(DBHelper.TBL_USER_FATCONTENT, fatcontent);
				long result = 0;
				if (dbHelper.isExistCardNoInDB(DBHelper.TBL_USER,
						app.currentCardNo)) {
					result = dbHelper.updateUserData(values);
				} else {
					result = dbHelper.insertData2DB(DBHelper.TBL_USER, values);
				}
				if (result < 0) {
					Toast.makeText(this, "save user data failed",
							Toast.LENGTH_LONG).show();
				} else {
					dbHelper.close();
					Intent intent2 = new Intent(this,
							ControlPanelActivity.class);
					startActivity(intent2);
					this.finish();
				}
			}
			break;

		case R.id.edit_back:
			Intent intent2 = new Intent(this, ControlPanelActivity.class);
			startActivity(intent2);
			this.finish();
			break;
		default:
			break;
		}
	}

	private boolean checkInputData() {

		if ("".equals(mUsrAge.getText().toString())
				|| Integer.parseInt(mUsrAge.getText().toString()) > 100
				|| Integer.parseInt(mUsrAge.getText().toString()) < 8) {
			Toast.makeText(this, R.string.edit_usrage_error, Toast.LENGTH_SHORT)
					.show();
			mUsrAge.requestFocus();
			return false;
		}

		if ("".equals(mUsrHeight.getText().toString().trim())
				|| Integer.parseInt(mUsrHeight.getText().toString().trim()) > 230
				|| Integer.parseInt(mUsrHeight.getText().toString().trim()) < 70) {
			Toast.makeText(this, R.string.edit_usrheight_error,
					Toast.LENGTH_SHORT).show();
			mUsrHeight.setText("");
			mUsrHeight.requestFocus();
			return false;
		}

		if ("".equals(mUsrWeight.getText().toString().trim())
				|| Integer.parseInt(mUsrWeight.getText().toString().trim()) > 150
				|| Integer.parseInt(mUsrWeight.getText().toString().trim()) < 30) {
			Toast.makeText(this, R.string.edit_usrweight_error,
					Toast.LENGTH_SHORT).show();
			mUsrWeight.setText("");
			mUsrWeight.requestFocus();
			return false;
		}

		if ("".equals(mUsrFatcontent.getText().toString().trim())) {
			Toast.makeText(this, R.string.edit_usrfatcontent_error,
					Toast.LENGTH_SHORT).show();
			mUsrFatcontent.setText("");
			mUsrFatcontent.requestFocus();
			return false;
		}

		return true;
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(this, ControlPanelActivity.class);
		startActivity(intent);
		this.finish();
	}

	@Override
	public void onResume() {
		super.onResume();
		LayoutParams mUsrNmlp,mUsrAgelp,mUsrHeightlp,mUsrWeightlp,mUsrFatcontentlp;        
		mUsrNmlp=mUsrNm.getLayoutParams();
		mUsrAgelp=mUsrAge.getLayoutParams();
		mUsrHeightlp=mUsrHeight.getLayoutParams();
		mUsrWeightlp=mUsrWeight.getLayoutParams();
		mUsrFatcontentlp=mUsrFatcontent.getLayoutParams();
		mUsrNmlp.height=EditH;
		mUsrAgelp.height=EditH;
		mUsrHeightlp.height=EditH;
		mUsrWeightlp.height=EditH;
		mUsrFatcontentlp.height=EditH;
		mUsrNm.setLayoutParams(mUsrNmlp);
		mUsrAge.setLayoutParams(mUsrAgelp);
		mUsrHeight.setLayoutParams(mUsrHeightlp);
		mUsrWeight.setLayoutParams(mUsrWeightlp);
		mUsrFatcontent.setLayoutParams(mUsrFatcontentlp);
	}
}
